'%!in%' <- function(x,y)!('%in%'(x,y))

library(ggplot2)
library(stargazer)


# Load Data ---------------------------------------------------------------
load("lc_dat.rdata")
# Specify DVs ------------------------------------------------------------
# combining to DV list
DVS <- c("book_divchar", "book_divhist", "book_discrim",              
         "book_teachrace", "tellkid_equal", "tellkid_whiteworkharder",
         "tellkid_othersfacediscrim", "tellkid_blackrespectpolice", "tellkid_historicalfigs",
         "tellkid_whitediscrim", "tellkid_whiteprivilege", "buy_divchar",
         "movie_divchar", "changeschool", "attendblm",                 
         "blmsign", "communitymeeting", "antiracism_wkid",
         "antiracism_parenting")


# Demog Only ------------------------------------------------------------------
# * Specify RHS -------------------------------------------------------------
RHS <- c("age_lucid_sc", "woman", "edu_sc", "inc_sc",
         "zip_white_prop", "zip_black_prop", "zip_med_inc_sc", "zip_college_prop")

# * Model Loop --------------------------------------------------------------
# Formatting DF to store coefficients and p-values
coef_mat <- array(NA, c(length(DVS), length(RHS) + 1))
coef_mat <- as.data.frame(coef_mat)
names(coef_mat) <- c("var", RHS)
coef_mat$var <- DVS
sig_mat <- coef_mat

# empty list to store individual model results
model_results <- list()

for(i in 1:nrow(coef_mat)){
  rhs <- paste(RHS, collapse = "+")
  m <- lm(as.formula(paste(coef_mat$var[i], rhs, sep = "~")), 
          data = lc_dat)
  m_coef <- summary(m)$coefficients
  # store coefficients excluding intercept
  coef_mat[i, -1] <- m_coef[-1, 1]
  # store p-values but for intercept
  sig_mat[i, -1] <- m_coef[-1, 4]
  
  model_results[[i]] <- m
}

# * Table -------------------------------------------------------------------
iv_labs <- c("Age",
             "Woman",
             "Education",
             "Income",
             "Zip: Prop White",
             "Zip: Prop Black",
             "Zip: Median Income",
             "Zip: Prop College Degree")
dv_labs <- c("Anti-Racist Parenting Workshop",
             "Anti-Racist Workshop w/kid",
             "Attend BLM Protest",
             "Make BLM Sign",
             "Book on Discrimination",
             "Book w/ diverse characters",
             "Historical Books",
             "Book on Teaching about Race",
             "Toys w/diverse characters",
             "Change School",
             "Attend Community Meeting",
             "Movie w/diverse characters",
             "Tell Kids: Respect Police",
             "Tell Kids: People Equal",
             "Tell Kids: Historical Figures",
             "Tell Kids: Others Face Discrimination",
             "Tell Kids: We Face Discrimination",
             "Tell Kids: White Privilege",
             "Tell Kids: Whites work Harder")

# model results by DV order
stargazer(model_results[c(19,18,15,16,3,1)],
          title = "Correlates of Parenting Decisions",
          dep.var.caption = "",
          covariate.labels = iv_labs,
          column.labels = dv_labs[1:6],
          dep.var.labels.include = F,
          header = F, initial.zero = F,
          type = "latex", label = "tab1_demog", 
          out = "table1_demog.tex",
          digits = 3, omit.stat = c("f", "adj.rsq"),  df = F, no.space = T)

stargazer(model_results[c(2, 4, 12, 14, 17, 13, 8)],
          title = "Correlates of Parenting Decisions",
          dep.var.caption = "",
          covariate.labels = iv_labs,
          column.labels = dv_labs[7:13],
          dep.var.labels.include = F,
          header = F, initial.zero = F,
          type = "latex", label = "tab2_demog", 
          out = "table2_demog.tex",
          digits = 3, omit.stat = c("f", "adj.rsq"),  df = F, no.space = T)

stargazer(model_results[c(5, 9, 7, 10, 11, 6)],
          title = "Correlates of Parenting Decisions",
          dep.var.caption = "",
          covariate.labels = iv_labs,
          column.labels = dv_labs[14:19],
          dep.var.labels.include = F,
          header = F, initial.zero = F,
          type = "latex", label = "tab3_demog", 
          out = "table3_demog.tex",
          digits = 3, omit.stat = c("f", "adj.rsq"),  df = F, no.space = T)

# Add Family and COVID ------------------------------------------------------------------
# * Specify RHS -------------------------------------------------------------
RHS <- c("age_lucid_sc", "woman", "edu_sc", "inc_sc",
         "zip_white_prop", "zip_black_prop", "zip_med_inc_sc", "zip_college_prop",
         "oldest_child_age_sc", "multkids", "childcaretime_sc", "sahp", 
         "covidwork_hours", "covidwork_left")


# * Model Loop --------------------------------------------------------------
# Formatting DF to store coefficients and p-values
coef_mat <- array(NA, c(length(DVS), length(RHS) + 1))
coef_mat <- as.data.frame(coef_mat)
names(coef_mat) <- c("var", RHS)
coef_mat$var <- DVS
sig_mat <- coef_mat

# empty list to store individual model results
model_results <- list()

for(i in 1:nrow(coef_mat)){
  rhs <- paste(RHS, collapse = "+")
  m <- lm(as.formula(paste(coef_mat$var[i], rhs, sep = "~")), 
          data = lc_dat)
  m_coef <- summary(m)$coefficients
  # store coefficients excluding intercept
  coef_mat[i, -1] <- m_coef[-1, 1]
  # store p-values but for intercept
  sig_mat[i, -1] <- m_coef[-1, 4]
  
  model_results[[i]] <- m
}

# * Table -------------------------------------------------------------------
iv_labs <- c("Age",
             "Woman",
             "Education",
             "Income",
             "Zip: Prop White",
             "Zip: Prop Black",
             "Zip: Median Income",
             "Zip: Prop College Degree",
             "Oldest Child's Age",
             "Multiple Kids",
             "Childcare Time",
             "Stay at home Parent",
             "COVID Reduced Hours",
             "COVID Left Workforce")
dv_labs <- c("Anti-Racist Parenting Workshop",
             "Anti-Racist Workshop w/kid",
             "Attend BLM Protest",
             "Make BLM Sign",
             "Book on Discrimination",
             "Book w/ diverse characters",
             "Historical Books",
             "Book on Teaching about Race",
             "Toys w/diverse characters",
             "Change School",
             "Attend Community Meeting",
             "Movie w/diverse characters",
             "Tell Kids: Respect Police",
             "Tell Kids: People Equal",
             "Tell Kids: Historical Figures",
             "Tell Kids: Others Face Discrimination",
             "Tell Kids: We Face Discrimination",
             "Tell Kids: White Privilege",
             "Tell Kids: Whites work Harder")

# model results by DV order
stargazer(model_results[c(19,18,15,16,3,1)],
          title = "Correlates of Parenting Decisions",
          dep.var.caption = "",
          covariate.labels = iv_labs,
          column.labels = dv_labs[1:6],
          dep.var.labels.include = F,
          header = F, initial.zero = F,
          type = "latex", label = "tab1_demogFam", 
          out = "table1_demogFam.tex",
          digits = 3, omit.stat = c("f", "adj.rsq"),  df = F, no.space = T)

stargazer(model_results[c(2, 4, 12, 14, 17, 13, 8)],
          title = "Correlates of Parenting Decisions",
          dep.var.caption = "",
          covariate.labels = iv_labs,
          column.labels = dv_labs[7:13],
          dep.var.labels.include = F,
          header = F, initial.zero = F,
          type = "latex", label = "tab2_demogFam", 
          out = "table2_demogFam.tex",
          digits = 3, omit.stat = c("f", "adj.rsq"),  df = F, no.space = T)

stargazer(model_results[c(5, 9, 7, 10, 11, 6)],
          title = "Correlates of Parenting Decisions",
          dep.var.caption = "",
          covariate.labels = iv_labs,
          column.labels = dv_labs[14:19],
          dep.var.labels.include = F,
          header = F, initial.zero = F,
          type = "latex", label = "tab3_demogFam", 
          out = "table3_demogFam.tex",
          digits = 3, omit.stat = c("f", "adj.rsq"),  df = F, no.space = T)
